package org.jheuristics.ga.operators.selectors;

import org.jheuristics.Individual;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Population;
import org.jheuristics.ga.multiObjective.NormalizeFitness;
import org.jheuristics.ga.operators.Selector;

/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Oct 28, 2005
 */
public abstract class AbstractNormalizeFitnessSelector implements Selector {
	private NormalizeFitness normalizeFitness;

	/**
	 * TODO
	 *
	 * @param densityAlgorithm
	 */
	public AbstractNormalizeFitnessSelector(NormalizeFitness normalizeFitness) {
		setNormalizeFitness(normalizeFitness);
	}

	/**
	 * TODO
	 *
	 * @param population
	 * @param howMany
	 * @param status
	 * @param config
	 * @return
	 * @see org.jheuristics.ga.operators.Selector#select(org.jheuristics.ga.Population, int, org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	public Individual[] select(Population population, int howMany, GAStatus status, GAConfig config) {
		if (null == population) {
			throw new NullPointerException();
		}
		if (0 > howMany) {
			throw new IllegalArgumentException();
		}
		if (0 == howMany) {
			return new Individual[0];
		}
		if (population.size() <= howMany) {
			return population.toIndividualArray();
		}
		return select(population.toIndividualArray(), howMany, status, config);
	}

	/**
	 * TODO
	 *
	 * @param individuals
	 * @param howMany
	 * @param status
	 * @param config
	 * @return
	 */
	protected abstract Individual[] select(Individual[] individuals, int howMany, GAStatus status, GAConfig config);

	/**
	 * TODO
	 *
	 * @return
	 */
	public NormalizeFitness getNormalizeFitness() {
		return normalizeFitness;
	}

	/**
	 * TODO
	 *
	 * @param normalizeFitness
	 */
	public void setNormalizeFitness(NormalizeFitness normalizeFitness) {
		if (null == normalizeFitness) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		this.normalizeFitness = normalizeFitness;
	}

}
